/*
 * 表格中td 操作，键盘上下左右键控制td焦点。
 */

var fangxiang;
function controlTdEvent(tableId)
{
	$("#"+tableId).find("td").keydown(function(e)
	{
		if (e.which === 39 || e.which === 13) // ---->向右键
		{
			debugger;
//			if($(".layui-layer-dialog").find(".layui-layer-title").length < 1){ //西药品如果提示皮试等，不让他弹出
			if($(".smart_search").length<1){
				e.preventDefault();  
				var next = $(e.target).parents("td").next();
				fangxiang = "you";
				beforeAndAfter(next);
			}
//			}
		}
		else if (e.which === 37)
		{// <----向左键
			var next = $(this).prev();
			fangxiang = "zuo";
			beforeAndAfter(next);
		}
		else if(e.which === 38)//向上箭头
		{
			if($(".smart_search").length<1){
				
				var className = $(this).find("textarea").attr("class");
				$(this).parents("tr").prev().find("."+className).focus();
				//聚焦的时候，activeid 赋值
				 activeAdviceInputId = $(this).parents("tr").prev().find("textarea").attr("id");
			}
		}
//		else if(e.which === 40)// 按向下箭头
//		{
//			debugger;
//			if($(".smart_search").is(":hidden")){
//				
//				var className = $(this).parents("tr").find("textarea").attr("class");
//				$(this).parents("tr").next().find("."+className).focus();
////				$("#"+activeAdviceInputId).parents("tr").next().find("."+className).focus();
//				//聚焦的时候，activeid 赋值
//				activeAdviceInputId = $(this).parents("tr").next().find("textarea").attr("id");
//			}
//		}
	})
}

function beforeAndAfter(next)
{
	var flag;
	var next2; //最终focus 的元素
	var temp = next.children();
	if(temp.length>0 && temp.is(":visible")){
		
		flag = next.children(":eq(0)")[0].nodeName;
		next2 =  next;
	}else {
		if (fangxiang=="you")
		{
			next2 =next.next();
			if(next2.children().length>0){
				
				flag = next2.children(":eq(0)")[0].nodeName;
			}else{
				beforeAndAfter(next2);
			}
		}
		else if(fangxiang=="zuo")
		{
			next2 =next.prev();
			flag = next2.children(":eq(0)")[0].nodeName;
		}
	}
	
	 if (next2.length == 0)
	{
		 if(next2.parents("tr").next().length>0){
			 addCurRowEvent();
		 }
	}else{
		
		if (flag == "INPUT")
		{
			var isReadOnly = next2.find("input").attr("readonly");
			if(isReadOnly){
				if (fangxiang=="you"){
					next2 = next2.next();
					
				}else if (fangxiang=="zuo"){
					next2 = next2.prev();
				}
				beforeAndAfter(next2)
			}else{
				
				next2.find("input").focus();
				next2.find("input").select();
			}
			
		}else if(flag == "TEXTAREA"){
			next2.find("textarea").focus();
			next2.find("textarea").select();
		}
		else if (flag == "SELECT")
		{
			next2.find("select").select2("open");
			select2Event(next2);
		}
		else if (flag == "BUTTON")
		{
			if (fangxiang=="you"){
				next2 = next2.next();
				
			}else if (fangxiang=="zuo"){
				next2 = next2.prev();
			}
			if(next2.length>0){
				beforeAndAfter(next2);
			}
			else{//中药表格单独处理
				controlChinaTable(next);
			}
		}
	}
}
function controlChinaTable(next){
	var cur_table = $(next).parents("tbody").attr("class");
	var cur_tr =  $(next).parents("tr").index();
	if(cur_table=="china_medicine_tbody1"){
		var nextcontrol = $(".china_medicine_tbody2").find("tr:eq("+cur_tr+")");
		if(nextcontrol.length>0){
			nextcontrol.find(".zhongyao_yznr").focus();
		}else{
			addCurRowEvent();
		}
	}else{
		
		var nextcontrol = $(".china_medicine_tbody1").find("tr:eq("+(cur_tr+1)+")");
		if(nextcontrol.length>0){
			nextcontrol.find(".zhongyao_yznr").focus();
		}else{
			addCurRowEvent();
		}
	}
	
}
function select2Event(next)
{
	next.find("select").on("select2:close", function()
	{
		var next1;
		if (fangxiang=="you")
		{
			next1 = next.next();
		}
		else if(fangxiang=="zuo")
		{
			next1 = next.prev();
		}
		if(next1.length > 0){
			beforeAndAfter(next1);
			
		}else{
			 if(next.parents("tr").next().length > 0 ){
				 $(next).parents("tr").next().find("textarea").focus();
			 }else{
				 addCurRowEvent();
			 }
		}
//		var flag1 = next1.children(":eq(0)")[0].nodeName;
//		if (flag1 == "INPUT")
//		{
//			next1.find("input").focus();
//		}
//		else if (flag1 == "SELECT")
//		{
//			next1.find("select").select2("open");
//			select2Event(next1);
//		}else if (next1.length == 0)
//		{
//
//		}
	})
}

function addCurRowEvent(){
	
	
	if(topTab=="Xiyao")
	{
		xy_addTrBtnEvent();
		
	}
	else if(topTab=="Zhongyao")
	{
		zy_addTrBtnEvent();
	}
	else if(topTab=="Jianyan")
	{
		jy_addTrBtnEvent();
	}
	else if(topTab=="Jiancha")
	{
		jc_addTrBtnEvent();
	}
	else if(topTab=="Shoushu")
	{
		
	}
	else if(topTab=="Zhiliao")
	{
		zl_addTrBtnEvent();
	} 
	else if(topTab=="Cailiao")
	{
		cl_addTrBtnEvent();
	} 
}













